//
//  EventStatistic.m
//  runtimeDemo
//
//  Created by admin on 2018/2/26.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "EventStatistic.h"
#import "Aspects.h"
#import "AspectTrackViewController.h"

typedef void (^AspectHandlerBlock)(id<AspectInfo> aspectInfo);

@implementation EventStatistic

+ (void)setupLogging {
    NSDictionary *config = @{
                             @"AspectTrackViewController": @{
                                     GLLoggingPageImpression: @"page imp - main page",
                                     GLLoggingTrackedEvents: @[
                                             @{
                                                 GLLoggingEventName: @"button one clicked",
                                                 GLLoggingEventSelectorName: @"buttonClicked",
                                                 GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) {
                                                     NSLog(@"button one clicked");
                                                 },
                                                 },
                                             @{
                                                 GLLoggingEventName: @"button tow clicked",
                                                 GLLoggingEventSelectorName: @"buttonClicked2",
                                                 GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) {
                                                     NSLog(@"button two clicked");
                                                 },
                                                 },
                                             @{
                                                 GLLoggingEventName: @"button three clicked",
                                                 GLLoggingEventSelectorName: @"buttonClicked3",
                                                 GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) {
                                                     NSLog(@"button three clicked");
                                                 },
                                                 },
                                             ],
                                     },
                             };
    [EventStatistic setupWithConfiguration:config];
}

+ (void)setupWithConfiguration:(NSDictionary *)dic {
    [UIViewController aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *className = NSStringFromClass([[aspectInfo instance] class]);
            NSString *pageImp = dic[className][@"GLLoggingPageImpression"];
            if (pageImp) {
                NSLog(@"%@", pageImp);
            }
        });
    } error:NULL];
    
    for (NSString *className in dic) {
        Class clazz = NSClassFromString(className);
        NSDictionary *config = dic[className];
        
        if (config[GLLoggingTrackedEvents]) {
            for (NSDictionary *event in config[GLLoggingTrackedEvents]) {
                SEL seleKor = NSSelectorFromString(event[GLLoggingEventSelectorName]);
                AspectHandlerBlock block = event[GLLoggingEventHandlerBlock];
                
                [clazz aspect_hookSelector:seleKor withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                        block(aspectInfo);
                    });
                } error:NULL];
            }
        }
    }
    
}

//+ (void)trackEventWithClass:(Class)kClass selector:(SEL)selector event:(NSString *)event {
//    [kClass aspect_hookSelector:selector withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) {
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            
//        });
//    } error:NULL];
//}


@end
